﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using System.Drawing;
using System.Net;
using System.Text.RegularExpressions;
using System.IO;

namespace Phong_CTSV
{
    public partial class ClientForm : System.Web.UI.UserControl
    {   
        Form form = null;
        String formName = null;
        long uni=0;
        List<Columns_Header> columnsHeader = null;
        List<Rows_Header> rowsHeader = null;
        Data_Table[,] dataTable = null;
        Data_Table[] Sum = null;
        int numColumn = 0;
        int numRow = 0;
        QuerrySQL query = new QuerrySQL();
        int year = 2011;
        University u;

        String userName;
        public ClientForm()
            : base()
        {

        }

        public Form formInfo
        {
            get
            {
                return form;
            }
        }

        public List<Columns_Header> ColumnsHeaderList
        {
            get
            {
                return columnsHeader;
            }
        }

        public List<Rows_Header> RowsHeaderList
        {
            get
            {
                return rowsHeader;
            }
        }

        public Data_Table[,] DataTable
        {
            get
            {
                return dataTable;
            }
        }

        public int NumRow
        {
            get
            {
                return numRow;
            }
        }

        public int NumColumn
        {
            get
            {
                return numColumn;
            }
        }

        public String FormName
        {
            get
            {
                return formName;
            }

            set
            {
                formName = value;

            }
        }
        public Data_Table[] SumValue
        {
            get { return Sum; }
        }
        private void getNumRowAndColumn()
        {
            foreach (Rows_Header rh in rowsHeader)
            {
                if (!rh.Has_child)
                {
                    numRow++;
                }
                
            }

            foreach (Columns_Header ch in columnsHeader)
            {
                if (!ch.Has_child)
                {
                    numColumn++;
                }
            }

            numColumn--;
        }

        private void getDataTable(long uni)
        {
            dataTable = new Data_Table[numRow,numColumn];
            
            for (int i = 0; i< numColumn;i++)
            {
                for (int j=0;j<numRow;j++)
                {
                    dataTable[j,i] = query.Select_Data_Cell(form.Id, uni, i, j, year);
                }
            }
        }
        
        public bool IsSum = false;
        private void getDataTable()
        {
            String fName = "Bảng " + Request["Form"];
            dataTable = Collection.Collection_Value(fName, year);
            
        }

        //----------------
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["userName"] != null)
            {
                u =  query.Select_UniversityByUser(Session["userName"].ToString());
                uni =u.ID;
                userName = Session["userName"].ToString();
                formName = "Bảng " + Request["Form"];
                form = query.Select_Form(formName);
                AddYear();
                year = int.Parse(ddlYear.SelectedValue);
                rowsHeader = query.Select_RowsHeaderByForm(formName);
                columnsHeader = query.Select_ColumnsHeaderByForm(formName);
                getNumRowAndColumn();
                if (userName == "admin")
                    getDataTable();
                else
                    getDataTable(uni);
                        
                
            }
            else
            {
                Response.Redirect("~/LoginPage.aspx");
            }

        }

        private void AddYear()
        {
            for (int i = 2011; i <= 2020; i++)
            {
                ddlYear.Items.Add(i.ToString());
            }
            for(int j =0;j <10;j++)
                if (ddlYear.Items[j].ToString() == (System.DateTime.Now.Year.ToString()))
                {
                    ddlYear.SelectedIndex = j;
                    break;
                }
        }

        protected void Submit_Click(object sender, EventArgs e)
        {
            if (!IsSum && userName != "admin")
            {
                Submit.Text = "Gửi";
                for (int i = 0; i < numColumn; i++)
                {
                    for (int j = 0; j < numRow; j++)
                    {

                        dataTable[j, i].Value = Request.Form[dataTable[j, i].Code_name];
                    }
                }

                for (int i = 0; i < numColumn; i++)
                {
                    for (int j = 0; j < numRow; j++)
                    {
                        query.Update_DataCell(formName, uni, System.DateTime.Today.Year, i, j, dataTable[j, i].Value);
                    }
                }
            
            }
            Sum = Total_Data.TotalValue(dataTable, numRow, numColumn);
            IsSum = true; 
              
           
        }
        
        protected void Download(object sender, EventArgs e)
        {
            try
            {

                Response.Clear();
                Response.Buffer = true;
                Response.Charset = "UTF-8";
                
                Response.ContentType = "application/vnd.ms-excel";
                Response.AppendHeader("Content-disposition", "attachment; filename=test.xls");
                Response.Flush();
                this.EnableViewState = false;
                Sum = Total_Data.TotalValue(dataTable, numRow, numColumn);
                Export export = new Export(dataTable, Sum, userName, form.Name, year);
                String sp = export.CreateExcelFile();
                
                Response.Write(sp);
                Response.End();
            }
            catch { }
        }

    }
}